﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace SystemX.Infrastructure
{
	/// <summary>
	/// Linq filter for domain objects.
	/// Inspired by :http://code.google.com/p/autofac/source/browse/trunk/src/Example/Remember.Model/Specification.cs
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public abstract class FilterX<T>
	{
		/// <summary>
		/// whether the item is fit to the current filter condition
		/// </summary>
		/// <param name="candidate"></param>
		/// <returns></returns>
		public bool Fit(T candidate)
		{
			return ApplyOn(new[] { candidate }).Any();
		}

		public IEnumerable<T> ApplyOn(IEnumerable<T> candidates)
		{
			Check.Require(candidates != null, "ArgumentNullException on 'candidates'");
			return ApplyOn(candidates.AsQueryable());
		}

		public abstract IQueryable<T> ApplyOn(IQueryable<T> candidates);

	}
}
